﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Marta.buchalska.wesele.Admin
{
    public partial class PhotoGallery : System.Web.UI.Page
    {

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataModel.WeedingDBDataContext context = new DataModel.WeedingDBDataContext();
            var categoryID = (int)e.Keys[0];
            var category = context.Photo_categories.FirstOrDefault(d => d.ID == categoryID);

            FileMeneger meneger = new FileMeneger();

            if (category.Photo_galleries.Count > 0)
                for (int i = 0; i < category.Photo_galleries.Count; i++)
                {
                    context.Photos.DeleteAllOnSubmit(category.Photo_galleries[i].Photos);
                    context.Photo_galleries.DeleteOnSubmit(category.Photo_galleries[i]);
                    meneger.DeleteGallery(Server.MapPath("~/img/" + category.Photo_galleries[i].Name));
                }

            context.Photo_categories.DeleteOnSubmit(category);

            context.SubmitChanges();
        }
    }
}